//
//  SLCommonsMapsDisplay.m
//  SecretRoom
//
//  Created by Sitdhibhong Laokok on 9/12/54 BE.
//  Copyright 2554 App Tree. All rights reserved.
//

#import "SLCommonsMapsDisplay.h"

@implementation SLCommonsMapsDisplay

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
    }
    
    return self;
}



+ (NSString *)urlMapsWithLocation:(CLLocationCoordinate2D)location displayWithZoomLevel:(NSInteger)zoomLevel size:(CGSize)size
{
    NSMutableString *googleStaticMapsLocation = [[NSMutableString alloc] initWithString:kGoogleStaticMapsURL];
    
    if ( ! CLLocationCoordinate2DIsValid(location) ) {
        [googleStaticMapsLocation release];
        return nil;
    }
    
    if ( zoomLevel <= 0 || zoomLevel >= 24 ) {
        [googleStaticMapsLocation release];
        return nil;
    }
    
    if ( CGSizeEqualToSize(size, CGSizeZero) ) {
        [googleStaticMapsLocation release];
        return nil;
    }
    
    [googleStaticMapsLocation appendFormat:@"?center=%f,%f&zoom=%i&size=%ix%i&maptype=roadmap&markers=color:blue|label:E|%f,%f&sensor=true", location.latitude, location.longitude, zoomLevel, (int) size.width, (int) size.height, location.latitude, location.longitude];
    
    return [googleStaticMapsLocation autorelease];
}

+ (UIImage *)staticMapsWithLocation:(CLLocationCoordinate2D)location displayWithZoomLevel:(NSInteger)zoomLevel size:(CGSize)size
{
    NSString *locationURL = [SLCommonsMapsDisplay urlMapsWithLocation:location displayWithZoomLevel:zoomLevel size:size];
    
    if (locationURL != nil) {
        return [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:locationURL]]];
    } else {
        return nil;
    }
    
}

+ (NSString *)googleMapsQueryStringURLWithLocation:(CLLocationCoordinate2D)location displayWithZoomLevel:(NSInteger)zoomLevel
{
    NSMutableString *googleQueryString = [[NSMutableString alloc] initWithString:kSLCommonsMapsDisplayGoogleMapsQueryURL];
    [googleQueryString appendFormat:@"?q=%fx%f&z=%i&hl=en&t=m", location.latitude, location.longitude, zoomLevel];
    
    return [googleQueryString autorelease];
}

@end
